🐍 Activity 1 – Understanding Lists
🔎 Understanding Lists 1: What is a List?
A Python list is like a variable but it can contain more than one item
dogs = ["pug", "cockerpoo", "labradoodle", "dachsund", "jack russell"]
🔎 Understanding Lists 2: Index Numbers
We can access any item in a Python list via its index number
dogs = ["pug", "cockerpoo", "labradoodle", "dachsund", "jack russell"] print(dogs[1]) # This outputs cockerpoo print(dogs[3]) # This outputs dachsund
🔎 Understanding Lists 3: Curious properties
A curious property of a Python list is that:
- The index number of the first item is 0
- The index number of the last item is 4
# index 0 1 2 3 4 dogs = ["pug", "cockerpoo", "labradoodle", "dachsund", "jack russell"] print(dogs[0]) # This outputs pug print(dogs[4]) # This outputs jack russell
🐍 Activity 1 - Coding Challenges
Open the file called Activity 1.py
- Read the instruction for each challenge
- Then complete it
📖 Activity 2 – Recapping For Loops
🔎 Recap - How a for loop works (1)
- You now know a for loop is controlled by a special variable
- This special variable is called the loop counter variable
- We will generally use the letter i for this variable; however it can be anything
⬇️ for i in range(1,11):
🔎 Recap - How a for loop works (2)
- We use the range() function to decide how many times a for loop will iterate (repeat)
- This loop begins with i being set to 0
⬇️
for i in range(0, 4):
i = 0
🔁
i = 1
🔁
i = 2
🔁
i = 3
🔁
i = 4 ... END!
🔎 Recap - How a for loop works (3)
- If we place lines code inside the for loop
- They will be executed each iteration
for i in range(0, 4):
print("Penguins")
print("--------")
Penguins -------- Penguins -------- Penguins -------- Penguins --------
🔎 Recap - How a for loop works (4)
- We can also output the value of i in each iteration
for i in range(0, 4):
print(f"i is currently {i}")
print("i will now increase by 1")
print("i reached 5 - end of loop")
i is currently 0 i will now increase by 1 i is currently 1 i will now increase by 1 i is currently 2 i will now increase by 1 i is currently 3 i will now increase by 1 i reached 5 - end of loop
📝 Activity - Answer Questions about For Loops
- Open the file called (2) Activity 2.md
- Complete the questions
🐍 Activity 3 – Use a for loop to iterate through a list
🔎🐍 (1) Remember how to output each item in a list
You have been given a list of European Capitals
capitals = ["Paris", "Berlin", "Madrid", "Lisbon", "Brussells", "Amsterdam"]
- Output the first three items as part of a sentence
Paris is a European Capital Berlin is a European Capital Madrid is a European Capital
Click to see how the code should look
print(f"{capitals[0]} is a European Capital")
print(f"{capitals[1]} is a European Capital")
print(f"{capitals[2]} is a European Capital")
🔎🐍 (2) Comment out the code you have just written
- You will now learn how to do this in an easier way (not needing to write each output separately)
- Firstly you must comment-out each of your print functions
- This will ensure they are still there but won't execute when you want to run further code
Click to see how the code should look
Literally just put a hash tag at the start of each line
#print(f"{capitals[0]} is a European Capital")
#print(f"{capitals[1]} is a European Capital")
#print(f"{capitals[2]} is a European Capital")
🔎🐍 (3) Create a for loop to display each capital
- Create a for loop with a range 0 - 5
- Place the code to print only one city within it
- Run the code to see what happens
- It's not quite where we want it...
Click to see how the code should look
for i in range(0, 5):
print(f"{capitals[0]} is a European Capital")
🔎🐍 (4) Changing the number in the square brackets
The loop repeatedly outputs Paris!
This is because we have just told it to output capitals[0] for each iteration
- Change the number 0 for the loop counter variable
- As the loop counter variable changes after each iteration, it will output a different city
Click to see how the code should look
for i in range(0, 5):
print(f"{capitals[i]} is a European Capital")
🐍 Activity 4 – Use a for loop to combine values in two lists
🔎🐍 (1) Create a for loop
You have been given:
- A list of European Capitals
- A list of countries that match each capital
capitals= ["Paris", "Dublin", "London", "Madrid", "Lisbon", "Berlin"] countries = ["France", "Ireland", "England", "Spain", "Portugal", "Germany"]
Paris is the capital of France Dublin is the capital of Ireland London is the capital of England etc...
Click to see how the code should look
for i in range(0, 6):
print(f"{capitals[i]} is the capital of {countries[i]}")